package com.jn.photogallery.common;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ProgressBar;

import com.jn.photogallery.R;


/**
 * 自定义控件——加载中Dialog
 */
public class LoadingDialog extends Dialog implements View.OnClickListener {
    private Context mContext;
    private View mView;
    private ProgressBar mProgressBar;

    public LoadingDialog(Context context) {
        super(context, R.style.transparent_dialog);
        mContext = context;
        initView();
        setCanceledOnTouchOutside(false);
    }

    private void initView() {
        mView = LayoutInflater.from(mContext).inflate(R.layout.loading_dialog, null);
        mProgressBar = mView.findViewById(R.id.prgrs);
        Animation animation = new RotateAnimation(0, 359, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(2000);
        animation.setRepeatCount(Animation.INFINITE);
        mProgressBar.startAnimation(animation);
        getWindow().setContentView(mView);
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    @Override
    public void show() {
        if (!isShowing()) {
            Window window = getWindow();
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
            window.setAttributes(layoutParams);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            super.show();
        }
    }

    @Override
    public void dismiss() {
        if (isShowing()) {
            if (null != mContext) {
                super.dismiss();
            }
        }
    }
}
